//polynomial divide in GF(p)
//a, b are row vectors of integer in the range [0, p-1].
//a, b, Q, R are row vectors that represent polynomials. The elements are coefficients of polynomials. for example, vector b represents a polynomial b(1) + b(2)*X + ... b(n)*X^(n-1).
//the function return quotient and remainder of a divided by b in GF(p).
// For example, these statements always return true.
// [Q, R] = gfdeconv(a, b, p)
// tmp = gfconv(b, Q, p)
// tmp1 = gfadd( tmp, R, p)
// tmp1 == a 
function [Q, R] = gfdeconv(a, b, p)
  [lhs rhs] = argn();
  if rhs < 3
    p = 2;
  end
  if p < 2 | p ~= int(p)
    error('p must be an prime number');
  end
  if max(a) > p-1 | max(b) > p-1 | min(a) < 0 | min(b) < 0
    error('elements of a and b must integers in the range [0, p-1]')
  end
  if a ~= int(a)
    error('elements of a and b must integers in the range [0, p-1]')
  end
  if b ~= int(b)
    error('elements of a and b must integers in the range [0, p-1]')
  end
  if size(a, 1) ~= 1 | size(b,1) ~= 1
    error('a and b must be row vectors');
  end

  a = poly(a, "x", "coeff");
  b = poly(b, "x", "coeff");
  [R,Q] = pdiv(a, b)
  R = coeff(R);
  Q = coeff(Q);
  R = pmodulo(R, p);
  Q = pmodulo(Q, p);
endfunction

